#include "clientwidget.h"
#include "ui_clientwidget.h"

#include <QMessageBox>
#include <QLineEdit>
#include <QFileDialog>

#include "ip_port.h"



ClientWidget::ClientWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ClientWidget)
{
    ui->setupUi(this);
    sockfd_ = nullptr;

    on_IP_Edit_editingFinished();

    //sockfd_ = new QTcpSocket(this);
}

ClientWidget::~ClientWidget()
{
    delete ui;
}

//输入ip
void ClientWidget::on_IP_Edit_editingFinished()
{
    ip_ = ui->IP_Edit->text();
    ui->textBrowser->setText(ip_); // 将我们输入的IP显示到列表中
    ui->IP_Edit->setText((ip_));
}

//输入port
void ClientWidget::on_lineEdit_editingFinished()
{
    port_ = ui->lineEdit->text();
    ui->textBrowser->append(port_);
}


//连接服务器
void ClientWidget::on_connectpushButton_clicked()
{
    qDebug() << ip_;
    qDebug() << port_;
    //检查Ip和port有效性
    if(!if_is_ip(ip_) || !if_is_port(port_)){
        QMessageBox::about(this, "About", tr("请输入正确的ip或端口号"));
    }
    sockfd_ = new QTcpSocket();

    //将得到的IP和端口转换成bytearray
    QByteArray ba = ip_.toLatin1();
    char *ip = ba.data();
    uint64_t port = port_.toInt();

    //链接服务器
    sockfd_->connectToHost(ip, port);

    if(!sockfd_->waitForConnected(30000)) //设定时器   避免粘包
    {
        qDebug() << "连接失败";
        ui->textBrowser->append(tr("服务器连接失败..."));
    }
    else
    {
        qDebug()<<("连接成功");
         ui->textBrowser->append(tr("连接服务器成功"));
    }
}


//上传文件槽函数
void ClientWidget::readmsg_up(){
    char buff[1024] = {0};
    sockfd_->read(buff,1023);

    qDebug() << buff+3;
    QString str = QString(QLatin1String(buff+3));
    qDebug() << str;

    if(strncmp(buff,"ok",2) == 0){
        ui->textBrowser->append(tr("recv connect success..."));
        ui->textBrowser->append((tr("start to send file...")));

        //开始发送数据
        QFile *localFile;
        localFile = new QFile(str);

        if(!localFile->open(QFile::ReadOnly)){
            qDebug() << "open file error";
            ui->textBrowser->append(tr("open file error..."));
            return;
        }

        ui->textBrowser->append(tr("open file success..."));
        char buf[256] = {0};
        QByteArray qb;
        while((localFile->read(buf,255)) > 0){
            qb = buf;
            sockfd_->write(qb);
        }
        ui->textBrowser->append(tr("upload finsh..."));
        sockfd_->flush();
    }
    else{
        ui->textBrowser->append(tr("upload error..."));
        return;
    }
}
//上传文件到服务器
void ClientWidget::on_uploadpushButton_clicked()
{
    qDebug() << "开始上传文件到服务器...";

    qint64 totalBytes = 0;
    QFile *localFile;

    //获取文件
    QString path = QFileDialog::getOpenFileName(this,"Open a file","/","files(*)");
    //获取文件信息
    QFileInfo info(path);

    QString upload = "选择上传的文件: " + path;
    ui->textBrowser->append(upload);

    localFile = new QFile(path);
    if(!localFile->open(QFile::ReadOnly)){
        qDebug() << "open file error...";
        return;
    }
    qDebug() << tr("文件打开成功!");

    //得到文件总大小
    totalBytes = localFile->size();
    qDebug() << "文件总大小: " << totalBytes;

    QString up = "upload";
    QString size = QString::number(totalBytes,10);
    up += " " + path + " " + info.fileName() + " " + size;

    qDebug() << info.fileName();

    QByteArray ba = up.toLatin1();
    char *ch = ba.data();

    sockfd_->write(ch);
    sockfd_->flush();

    //上传文件
    connect(sockfd_,SIGNAL(readRead()),this,SLOT(readmsg_up));

}


//下载文件
void ClientWidget::on_downloadpushButton_clicked()
{
    qDebug() << "开始下载文件...";
    ui->textBrowser->append(tr("start download file..."));

}


//列表展示
void ClientWidget::on_listpushButton_clicked()
{

}
